/**
 * 项目:thinknode
 * 采用：异步网络编程，具有高并发，高可靠性，自建常驻内存的session机制，摆脱传统session文件读取，mvc设计模式
 *      nunjucks模板引擎，简洁高效
 * 目录结构：
 *      untils:工具类目录
 *      home:前端               admin:后端
 *          controller               controller
 *          css                      css
 *          imgs                     imgs
 *          views                    views
 * 
 *      config:配置文件  
 *          db.js
 * 
 *      app.js:入口文件   
 */

 let http = require("http");
 let mysql = require("mysql");
 let fs = require("fs");

 http.createServer(function(req, res) {
    if(req.url=='/html/china') {
        res.setHeader('Content-type','text/html;charset=utf-8');
        //数据库操作
        let connection = mysql.createConnection({
            host:'127.0.0.1',
            user:'root',
            password:'123456',
            port:'3306',
            database:'yiqing',
        })
        connection.connect();
        connection.query("select * from china where `date`=?",'2022-03-10',function(err,rows){
            let html = '';
            for(let row in rows){
                html+='<tr>'
                   html+='<td>'+ rows[row].city+'</td>'
                   html+='<td>'+ rows[row].nums+'</td>'
                   html+='<td>'+ rows[row].date+'</td>'
                   html+='</tr>'
            }
            console.log(html);
            fs.readFile('./html/china.html', function(err, buffer){
                let outPutStr = buffer.toString();
                let newStr = outPutStr.replace('{{result}}',html);
                res.write(newStr);
                res.end();
            });
        });
    } else {
        res.end();
    }
 }).listen(8080);